angular.module('Controller')

.controller('QgyCorporateInformationCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            createTimeStart:undefined,
            createTimeEnd:undefined,
            buzType:undefined
        }
    initSearchData()
    $scope.nfReadFlagList=commonConstant.notificationReadFlagList(true)
    $scope.mnfBuzTypeList=commonConstant.notificationBuzTypeList(true)
    $scope.nfBuzTypeList=_.filter($scope.mnfBuzTypeList,(theItem)->theItem.value not in  [3, 4, 6])
    $scope.nfTypeList=commonConstant.notificationTypeList(true)
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            buzType:$scope.search.buzType
            userType:1
        }
        if $scope.search.createTimeStart
            reqData['createTimeStart']=moment($scope.search.createTimeStart).format('YYYY-MM-DD')
        if $scope.search.createTimeEnd
            reqData['createTimeEnd']=moment($scope.search.createTimeEnd).format('YYYY-MM-DD')
        $http.get('/notification',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,buzType:1,userType:1}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        viewData:(item)->
            return
            theData=angular.copy(item)
            showModal('view',theData)
        dealData:(item)->
            $rootScope.pageType='house-detail'
            $rootScope.pageParams={houseId:item.houseId,contractId:item.contractId}
            menuManage.goState('house-detail',true)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/operateApply/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('动态信息删除成功');
                    $scope.queryData();
                else
                    wfAlert.toastError(resp.message)
            )
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyCorporateEditCtrl',
            templateUrl: 'templates/business-manage/corporate-infomation/corporate-edit-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
            ''
        )
)